package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @create 2020-05-24 11:04
 */
@Controller
public class listController {
    @Autowired
    private ListFeignClient listFeignClient;

    //搜索页面 搜索传过来的是key:value显示
    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model){
        //入参的回显到页面
        model.addAttribute("searchParam",searchParam);
        //查询
        SearchResponseVo searchResponseVo = listFeignClient.list(searchParam);
        //1.品牌集合
        model.addAttribute("trademarkList",searchResponseVo.getTrademarkList());
        //2.平台属性集合
        model.addAttribute("attrsList",searchResponseVo.getAttrsList());
        //3.商品集合
        model.addAttribute("goodsList",searchResponseVo.getGoodsList());
        //4.分页
        model.addAttribute("pageNo",searchResponseVo.getPageNo());
        model.addAttribute("totalPages",searchResponseVo.getTotalPages());
        //一：UrlParam 点击后的条件添加
        String urlParam = makeUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //二：排序的显示，红底
        Map orderMap = makeOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);
        //三： 选中的品牌回显
        model.addAttribute("trademarkParam",makeTrademark(searchParam));
        //四：选中的平台属性回显
        model.addAttribute("propsParamList",makePropsParamList(searchParam));
        return "list/index";
    }
    //四：选中的平台属性回显
    private List<Map> makePropsParamList(SearchParam searchParam){
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0){
            Arrays.stream(props).map(prop -> {
                String[] p = prop.split(":");
                Map map = new HashMap<>();
                map.put("attrId",p[0]);
                map.put("attrValue",p[1]);
                map.put("attrName",p[2]);
                return map;
            }).collect(Collectors.toList());
        }
        return null;
    }

    //三： 选中的品牌回显
    private String makeTrademark(SearchParam searchParam){
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            String[] tk = trademark.split(":");
            return "品牌："+tk[1];
        }
        return null;
    }

    //二：排序的显示，红底
    private Map makeOrderMap(SearchParam searchParam){
        Map orderMap = new HashMap<>();
        //1.type：当前是按照综合还是价格进行排序
        String order = searchParam.getOrder();
        if(!StringUtils.isEmpty(order)){
            String[] p = order.split(":");//按什么排序对他进行切割
            orderMap.put("type",p[0]);
            orderMap.put("sort",p[1]);
        }else {
            orderMap.put("type","1");//默认按综合排序
            orderMap.put("sort","desc");
        }
        return orderMap;
    }


    //一：UrlParam 字符串拼接
    private String makeUrlParam(SearchParam searchParam){
        StringBuilder sb = new StringBuilder();
        String keyword = searchParam.getKeyword();
        if(StringUtils.isNotEmpty(keyword)){
            sb.append("keyword=").append(keyword);
        }
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            if(sb.length() > 0){//判断它是不是第一个条件
                sb.append("&trademark=").append(trademark);
            }else{
                sb.append("trademark=").append(trademark);
            }
        }
        Long category1Id = searchParam.getCategory1Id();
        if(null != category1Id){
            if(sb.length() > 0){
                sb.append("&category1Id=").append(category1Id);
            }else{
                sb.append("category1Id=").append(category1Id);
            }
        }
        Long category2Id = searchParam.getCategory2Id();
        if(null != category2Id){
            if(sb.length() > 0){
                sb.append("&category2Id=").append(category2Id);
            }else{
                sb.append("category2Id=").append(category2Id);
            }
        }
        Long category3Id = searchParam.getCategory3Id();
        if(null != category3Id){
            if(sb.length() > 0){
                sb.append("&category3Id=").append(category3Id);
            }else{
                sb.append("category3Id=").append(category3Id);
            }
        }
        //平台属性
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            for (String prop : props) {
                if(sb.length() > 0){
                    sb.append("&props=").append(prop);
                }else{
                    sb.append("props=").append(prop);
                }
            }
        }
        return "/list.html?"+sb.toString();
    }
}
